package org.eclipse.epsilon.emc.rdbms.core.sqlstatement;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.eclipse.epsilon.emc.rdbms.core.persistancebroker.PersistanceBroker;
import org.eclipse.epsilon.emc.rdbms.core.persistancemechanism.RelationalDatabase;



public class InsertSQLStatement extends SQLStatement
{
	public String BuildForObject()	
	{
		return null;
	}
	
	/** This method is to generate the SQL Statement to insert an element into the database
	*@param tableInfo - The resultset containing the database,schema and table information
	*@param columns The columns which are in the table
	*@param primaryKey The columns for the primaryKey
	*@param the metaData for the columns.
	*@param properties  - This is the hashmap of attributes and values that will be specified in the where clause.
	*@return statement - This is the generated SQL Statement to delete the object from the database
	*/	
	
	public String BuildForCriteria(ResultSet tableInfo,ResultSet columns, ResultSet primaryKey, ResultSet columnsMetaData, HashMap<String, Object> properties) throws SQLException 
	{
		RelationalDatabase connection = PersistanceBroker.connectTo();
		String  statement = "";	
		ArrayList<String> columnNames = getArrayListFromResultSet(columns,"COLUMN_NAME");        //Need this multiple times so its cheaper to do this once here
		ArrayList<String> primaryKeyNames = getArrayListFromResultSet(primaryKey,"COLUMN_NAME"); //Need this multiple times so its cheaper to do this once here
		
		while (tableInfo.next())
		{
			databaseName = tableInfo.getString("TABLE_CAT");
			schemaName = tableInfo.getString("TABLE_SCHEM");
			tableName = tableInfo.getString("TABLE_NAME");	
		}
		
		statement = connection.getClauseStringInsert()+databaseName+"."+schemaName+"."+tableName;				
		statement = statement+ connection.getClauseOpenBrackets();
		statement = statement + getStringFromArrayList(columnNames,primaryKeyNames);  //This list the name of the columns to be inserted with data.
		statement = statement+connection.getClauseCloseBrackets();
		statement = statement+connection.getClauseStringValues();
		statement = statement+connection.getClauseOpenBrackets();
		statement = statement + getValues(columnsMetaData,primaryKeyNames,tableName, properties);  //Will get the default value for the columns but will be overriden by any HashMap properties.
		statement = statement+connection.getClauseCloseBrackets();
		return statement; 
	}

	/** This is not implemented
	*/	
	@Override
	public String BuildForCriteria(ResultSet sqlTables, HashMap properties,HashMap<String, Object> props) 
	{
		return null;
	}

	/** This is not implemented
	*/	
	@Override
	public String BuildForCriteria(ResultSet sqlTables,HashMap<String, Object> props) throws SQLException 
	{	
		return null;
	}

	/** This is not implemented
	*/	
	@Override
	public String BuildForCriteria(ResultSet sqlTables,ArrayList<String> properties, HashMap<String, Object> props) 
	{
		return null;
	}

	/** This is not implemented
	*/	
	@Override
	public String BuildForCriteria(ResultSet sqlTables,
			ArrayList<String> columns, ArrayList PKtables, ArrayList FKtables,
			ArrayList PKcols, ArrayList FKcols,
			HashMap<String, Object> parameters) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}
}
